

class Auth {
    constructor({ app, env }) {
        this._app = app
        this._env = env
        this._store = app.$store
        
        this.authenticated = false // 是否已经登录成功
    }

    /**
     * 获取登录信息
     * @param {*} token 
     */
    static authenticateUser(token) {
        this._store.setItem('token', token)
    }

    /**
     * 判断登录状态
     * @returns {boolean}
     */
    static isUserAuthenticated() {
        return false
    }

    /**
     * 登录动作操作
     * @param {boolean} force 是否强制登录
     * @param {boolean} auto 是否静默登录
     */
    static authenticateAction(force, auto) {
        
        if (force && !this.isUserAuthenticated()) {
            // 涉及登录操作
            if (this._app.$wakeup) this.wakeupLogin().then(this.callback)
            else if (this._app.$sunrise) this._app.$sunrise.navigateTo('/pages/login/index')
            else window.location.href = '/login'
        } else if (auto && !this.isUserAuthenticated()) {
            // 静默登录
            this.autoLogin()
        } else {
        }
    }

    // 唤起登录弹窗
    static wakeupLogin() {
        return new Promise((resolve, reject) => {
            this._app.$wakeup.showLogin({
                success: (data) => {
                    resolve(data)
                },
                error: (err) => {
                    reject(err)
                }
            })
        })
    }

    static autoLogin() {

    }

    static callback({ uid, username, token, tokenExpired }) {
        const { BASE_URL, XSRF_HEADER } = this._env.config
        const domain = BASE_URL.slice(-9)
        if (this._app.isServer) {}
        else {
            Cookies.set('OP_ID', uid, { expires: new Date(tokenExpired), domain })
            Cookies.set('OP_NAME', username, { expires: new Date(tokenExpired), domain })
            Cookies.set(XSRF_HEADER, token, { expires: new Date(tokenExpired), domain })
        }
    }

    static set () {
        this.authenticated = true
        
    }

    static get () {}


}

export default Auth
